package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author Bright
 * @since 2022-04-09
 */
@Slf4j
@Api(tags = "用户借款")
@RestController
@RequestMapping("/api/core/borrowInfo")
public class BorrowInfoController {

    @Resource
    BorrowInfoService borrowInfoService;


    @ApiOperation(value = "得到借款人借款状态")
    @GetMapping("/auth/getBorrowInfoStatus")
    public R getBorrowInfoStatus(HttpServletRequest request) {

        Long userId = JwtUtils.getUserId(request.getHeader("token"));
        Integer borrowInfoStatus= borrowInfoService.getBorrowInfoStatusByUserId(userId);

        return R.ok().data("borrowInfoStatus",borrowInfoStatus);

    }

    @ApiOperation(value = "获取借款额度")
    @GetMapping("/auth/getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request){

        Long userId = JwtUtils.getUserId(request.getHeader("token"));
        BigDecimal borrowAmount= borrowInfoService.getBorrowAmountByUserId(userId);
        return R.ok().data("borrowAmount",borrowAmount);

    }

    @ApiOperation(value = "存入借款信息")
    @PostMapping("/auth/save")
    public R save(@RequestBody BorrowInfo borrowInfo,HttpServletRequest request){
        Long userId = JwtUtils.getUserId(request.getHeader("token"));
        borrowInfoService.saveBorrowInfo(borrowInfo,userId);

        return R.ok();

    }



}

